/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  com.alibaba.fastjson.JSONObject
 *  org.apache.http.HttpEntity
 *  org.apache.http.HttpResponse
 *  org.apache.http.client.methods.HttpPost
 *  org.apache.http.client.methods.HttpUriRequest
 *  org.apache.http.entity.StringEntity
 *  org.apache.http.entity.mime.HttpMultipartMode
 *  org.apache.http.entity.mime.MultipartEntityBuilder
 *  org.apache.http.impl.client.CloseableHttpClient
 *  org.apache.http.impl.client.HttpClients
 *  org.apache.http.util.EntityUtils
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.gys.common.util;

import com.alibaba.fastjson.JSONObject;
import com.gys.common.data.HttpJson;
import com.gys.controller.dataManger.TokenManger;
import java.io.File;
import java.nio.charset.Charset;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpClientUtil {
    private static final Logger log = LoggerFactory.getLogger(HttpClientUtil.class);

    public static HttpJson httpPostJson(String url, String body) {
        HttpJson httpJson = new HttpJson();
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httppost = new HttpPost(url);
        try {
            httppost.addHeader("Content-type", "application/json; charset=utf-8");
            httppost.addHeader("X-Token", TokenManger.xToken);
            httppost.setEntity((HttpEntity)new StringEntity(body, Charset.forName("UTF-8")));
            HttpResponse response = httpclient.execute((HttpUriRequest)httppost);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                String conResult = EntityUtils.toString((HttpEntity)response.getEntity());
                JSONObject resultObj = JSONObject.parseObject((String)conResult);
                String code = resultObj.getString("code");
                if ("0".equals(code)) {
                    httpJson.setSuccess(true);
                    httpJson.setMsg(resultObj.getString("data"));
                } else {
                    httpJson.setMsg(resultObj.getString("message"));
                }
            } else {
                httpJson.setMsg("\u63a5\u53e3\u8bf7\u6c42\u51fa\u9519!\u9519\u8bef\u7801\uff1a" + statusCode);
            }
        }
        catch (Exception e) {
            log.error("\u63a5\u53e3\u8bf7\u6c42\u51fa\u9519", (Throwable)e);
            e.printStackTrace();
            httpJson.setMsg("\u63a5\u53e3\u8bf7\u6c42\u51fa\u9519!");
        }
        return httpJson;
    }

    public static HttpJson httpPostFile(String url, File file) {
        HttpJson httpJson = new HttpJson();
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httppost = new HttpPost(url);
        try {
            httppost.addHeader("X-Token", TokenManger.xToken);
            MultipartEntityBuilder mEntityBuilder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.RFC6532);
            mEntityBuilder.addBinaryBody("file", file);
            httppost.setEntity(mEntityBuilder.build());
            HttpResponse response = httpclient.execute((HttpUriRequest)httppost);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                String conResult = EntityUtils.toString((HttpEntity)response.getEntity());
                JSONObject resultObj = JSONObject.parseObject((String)conResult);
                String code = resultObj.getString("code");
                if ("0".equals(code)) {
                    httpJson.setSuccess(true);
                    httpJson.setMsg(resultObj.getString("data"));
                } else {
                    httpJson.setMsg(resultObj.getString("message"));
                }
            } else {
                httpJson.setMsg("\u63a5\u53e3\u8bf7\u6c42\u51fa\u9519!\u9519\u8bef\u7801\uff1a" + statusCode);
            }
        }
        catch (Exception e) {
            log.error("\u63a5\u53e3\u8bf7\u6c42\u51fa\u9519", (Throwable)e);
            e.printStackTrace();
            httpJson.setMsg("\u63a5\u53e3\u8bf7\u6c42\u51fa\u9519!");
        }
        return httpJson;
    }
}

